#include "ProcessPool.hpp"

int main()
{
    Processpool pp;
    // 创建进程池，让子进程进入自己的工作流中，父进程得到channels
    pp.InitProcessPool([](int fd){
         while (true) 
         {
            int code = 0;
            //std::cout << "子进程正在被堵塞: " << getpid() << std::endl;
            ssize_t n = read(fd, &code, sizeof(code));
            if(n == sizeof(code))
            {
                std::cout << "成功唤醒子进程" << getpid() << std::endl;
                if(code >= 0 && code < task.size())
                {
                    task[code]();
                }else{
                    std::cerr << "任务码错误" << std::endl;
                }
            }
            else if(n == 0)
            {
                std::cout << "关闭退出: " << getpid() <<std::endl;
                break;
            }else{
                std::cerr << "read fd :" << fd << " error" << std::endl;
            }
        }
    });
    sleep(3);
    //进程控制
    std::cout << "父进程开始控制" << std::endl;
    pp.PolingCtrlProcess(5);

    //进程退出
    pp.ProcessWaint();

    return 0;
}